﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Win32;
using System.IO;


namespace sentence
{
    public partial class ConfigForm : Form
    {
        XmlDocument xmldoc;
        public ConfigForm()
        {
            InitializeComponent();
            if (File.Exists(@"config.xml"))
            {
                //读取config.xml的内容
                xmldoc = new XmlDocument();
                xmldoc.Load(@"config.xml");
                XmlNode node = xmldoc.GetElementsByTagName("AutoStart").Item(0);
                AutoStartCheckBox.Checked = (node.InnerText.ToString() == "True") ? true : false;
                node = xmldoc.GetElementsByTagName("AutoExit").Item(0);
                AutoExitCheckBox.Checked = (node.InnerText.ToString() == "True") ? true : false;
                node = xmldoc.GetElementsByTagName("Save").Item(0);
                SaveDesktopCheckBox.Checked = (node.InnerText.ToString() == "True") ? true : false;
                node = xmldoc.GetElementsByTagName("SavePath").Item(0);
                SavePathTextBox.Text = node.InnerText.ToString();
            }
        }

        private bool isAppAutorun
        {
            get
            {
                RegistryKey HKLM = Registry.CurrentUser;
                RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
                Object path = Run.GetValue(System.Windows.Forms.Application.ProductName);
                HKLM.Close();
                if (path != null)
                {
                    if (path.ToString() != "\"" + System.Windows.Forms.Application.ExecutablePath + "\"")
                    {
                        HKLM = Registry.CurrentUser;
                        Run = HKLM.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
                        Run.SetValue(System.Windows.Forms.Application.ProductName, "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                        HKLM.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                if (value != isAppAutorun)
                {
                    RegistryKey HKLM = Registry.CurrentUser;
                    RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
                    if (value == true)
                    {
                        try
                        {
                            Run.SetValue(System.Windows.Forms.Application.ProductName, "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                            HKLM.Close();
                        }
                        catch (Exception Err)
                        {
                            MessageBox.Show(Err.Message.ToString(), "设置为开机启动时项出错");
                            AutoStartCheckBox.Checked = isAppAutorun;
                        }
                    }
                    else if (value == false)
                    {
                        try
                        {
                            Run.DeleteValue(System.Windows.Forms.Application.ProductName);
                            HKLM.Close();
                        }
                        catch (Exception Err)
                        {
                            MessageBox.Show(Err.Message.ToString(), "删除开机启动时项出错");
                            AutoStartCheckBox.Checked = isAppAutorun;
                        }
                    }
                }
            }
        }


        private void SelectPathButton_Click(object sender, EventArgs e)
        {
            // 设置根在桌面
            FolderBrowserDialog SavePathBrowserDialog = new FolderBrowserDialog();
            SavePathBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
            // 允许在对话框中包括一个新建目录的按钮
            SavePathBrowserDialog.ShowNewFolderButton = true;
            // 设置对话框的说明信息
            SavePathBrowserDialog.Description = "请选择保存壁纸的目录";

            if (SavePathBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                SavePathTextBox.Text = SavePathBrowserDialog.SelectedPath.ToString();
            }
        }

        private void SaveDesktopCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (SaveDesktopCheckBox.Checked == true)
            {
                SelectPathButton.Enabled = true;
                SavePathTextBox.Enabled = true;
            }
            else
            {
                SelectPathButton.Enabled = false;
                SavePathTextBox.Enabled = false;
            }
        }

        private void CancelConfigButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveConfigButton_Click(object sender, EventArgs e)
        {
            //保存设置到config.xml文件中
            string AutoStart = AutoStartCheckBox.Checked.ToString(); 
            string AutoExit = AutoExitCheckBox.Checked.ToString();
            string Save = SaveDesktopCheckBox.Checked.ToString();
            string SavePath = SavePathTextBox.Text;
            xmldoc = new XmlDocument();
            XmlDeclaration xmldecl;
            xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmldoc.AppendChild(xmldecl);
            XmlElement xe1 = xmldoc.CreateElement("Sentence");
            xe1.SetAttribute("Author", "cc");
            XmlElement XML_AutoStart = xmldoc.CreateElement("AutoStart");
            XML_AutoStart.InnerText = AutoStart;
            xe1.AppendChild(XML_AutoStart);
            XmlElement XML_AutoExit = xmldoc.CreateElement("AutoExit");
            XML_AutoExit.InnerText = AutoExit;
            xe1.AppendChild(XML_AutoExit);
            XmlElement XML_Save = xmldoc.CreateElement("Save");
            XML_Save.InnerText = Save;
            xe1.AppendChild(XML_Save);
            XmlElement XML_SavePath = xmldoc.CreateElement("SavePath");
            XML_SavePath.InnerText = SavePath;
            xe1.AppendChild(XML_SavePath);
            xmldoc.AppendChild(xe1);
            xmldoc.Save("config.xml");
            //实现设置
            if (AutoStartCheckBox.Checked == true)
            {
                isAppAutorun = true;
            }
            else
            {
                isAppAutorun = false;
            }
            MessageBox.Show("设置保存成功\n将在下次启动生效","Sentence");
            this.Close();
        }
    }
}
